package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.cld.domain.CldProject;
import com.cleaningcloud.cld.domain.CldProjectMaterial;
import com.cleaningcloud.cld.domain.CldRegionMaterialDetails;
import com.cleaningcloud.cld.mapper.CldProjectMapper;
import com.cleaningcloud.cld.mapper.CldProjectMaterialMapper;
import com.cleaningcloud.cld.mapper.CldRegionMaterialDetailsMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldRegionMaterialMapper;
import com.cleaningcloud.cld.domain.CldRegionMaterial;
import com.cleaningcloud.cld.service.ICldRegionMaterialService;

/**
 * 业态物资清单Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2025-09-29
 */
@Service
public class CldRegionMaterialServiceImpl implements ICldRegionMaterialService 
{
    @Autowired
    private CldRegionMaterialMapper cldRegionMaterialMapper;

    @Autowired
    private CldRegionMaterialDetailsMapper cldRegionMaterialDetailsMapper;

    @Autowired
    private CldProjectMaterialMapper cldProjectMaterialMapper;

    @Autowired
    private CldProjectMapper cldProjectMapper;

    /**
     * 查询业态物资清单
     * 
     * @param regionMaterialId 业态物资清单主键
     * @return 业态物资清单
     */
    @Override
    public CldRegionMaterial selectCldRegionMaterialByRegionMaterialId(Long regionMaterialId)
    {
        CldRegionMaterial obj = cldRegionMaterialMapper.selectCldRegionMaterialByRegionMaterialId(regionMaterialId);

        CldRegionMaterialDetails queryDetails = new CldRegionMaterialDetails();
        queryDetails.setRegionMaterialId(obj.getRegionMaterialId());
        List<CldRegionMaterialDetails> details = cldRegionMaterialDetailsMapper.selectCldRegionMaterialDetailsList(queryDetails);
        if(!details.isEmpty()){
            obj.setDetailsList(details);
        }
        return obj;
    }

    /**
     * 查询业态物资清单列表
     * 
     * @param cldRegionMaterial 业态物资清单
     * @return 业态物资清单
     */
    @Override
    public List<CldRegionMaterial> selectCldRegionMaterialList(CldRegionMaterial cldRegionMaterial)
    {
        return cldRegionMaterialMapper.selectCldRegionMaterialList(cldRegionMaterial);
    }

    /**
     * 新增业态物资清单
     * 
     * @param cldRegionMaterial 业态物资清单
     * @return 结果
     */
    @Override
    public int insertCldRegionMaterial(CldRegionMaterial cldRegionMaterial)
    {
        cldRegionMaterial.setCreateTime(DateUtils.getNowDate());
        cldRegionMaterial.setCreateId(SecurityUtils.getUserId());
        cldRegionMaterial.setCreateName(SecurityUtils.getUsername());
        cldRegionMaterial.setFlag("0");
        cldRegionMaterialMapper.insertCldRegionMaterial(cldRegionMaterial);
        if(!cldRegionMaterial.getDetailsList().isEmpty()){
            for (CldRegionMaterialDetails details : cldRegionMaterial.getDetailsList()){
                details.setRegionMaterialId(cldRegionMaterial.getRegionMaterialId());
                cldRegionMaterialDetailsMapper.insertCldRegionMaterialDetails(details);
            }
        }
        if(null != cldRegionMaterial.getRegionMaterialId()){
            CldProject project = new CldProject();
            project.setProjectStatus(2L);
            project.setProjectBusiness(cldRegionMaterial.getRegionId());
            List<CldProject> projectList = cldProjectMapper.getList(project);
            if(!projectList.isEmpty()){
                for (CldProject obj : projectList){
                    //先清空项目阈值
                    cldProjectMaterialMapper.deleteCldProjectMaterialByProjectId(obj.getProjectId());

                    if(!cldRegionMaterial.getDetailsList().isEmpty()){
                        for (CldRegionMaterialDetails details : cldRegionMaterial.getDetailsList()){
                            CldProjectMaterial material = new CldProjectMaterial();
                            material.setProjectId(obj.getProjectId());
                            material.setCommodityId(details.getCommodityId());
                            material.setCommodityNum(500L);
                            cldProjectMaterialMapper.insertCldProjectMaterial(material);
                        }
                    }
                }
            }
            return 1;
        }
        return 0;
    }

    /**
     * 修改业态物资清单
     * 
     * @param cldRegionMaterial 业态物资清单
     * @return 结果
     */
    @Override
    public int updateCldRegionMaterial(CldRegionMaterial cldRegionMaterial)
    {
        if(!cldRegionMaterial.getDetailsList().isEmpty()){
            //1 -删除业态物资清单明细
            cldRegionMaterialDetailsMapper.deleteCldRegionMaterialDetailsByRegionMaterialId(cldRegionMaterial.getRegionMaterialId());

            //2 -新增业态物资清单明细
            for (CldRegionMaterialDetails details : cldRegionMaterial.getDetailsList()) {
                details.setRegionMaterialId(cldRegionMaterial.getRegionMaterialId());
                cldRegionMaterialDetailsMapper.insertCldRegionMaterialDetails(details);
            }

            //3 -删除项目物资阈值不在物资清单内的商品
            //获取该业态所有履约项目
            CldProject project = new CldProject();
            project.setProjectStatus(2L);
            project.setProjectBusiness(cldRegionMaterial.getRegionId());
            List<CldProject> projectList = cldProjectMapper.getList(project);
            if(!projectList.isEmpty()){
                for (CldProject obj : projectList){
                    CldRegionMaterial regionMaterial = new CldRegionMaterial();
                    regionMaterial.setRegionId(obj.getProjectBusiness());
                    List<CldRegionMaterial> regionMaterialList = cldRegionMaterialMapper.selectCldRegionMaterialList(regionMaterial);
                    if(!regionMaterialList.isEmpty()){
                        //删除原项目物资阈值数据
                        List<CldProjectMaterial> updateProjectMaterialList = cldProjectMaterialMapper.selectCldProjectMaterialByProjectId(obj.getProjectId());
                        if(!updateProjectMaterialList.isEmpty()){
                            for (CldProjectMaterial updateObj : updateProjectMaterialList){
                                CldRegionMaterialDetails newDetails = new CldRegionMaterialDetails();
                                newDetails.setRegionMaterialId(regionMaterialList.get(0).getRegionMaterialId());
                                newDetails.setCommodityId(updateObj.getCommodityId());
                                List<CldRegionMaterialDetails> list = cldRegionMaterialDetailsMapper.selectCldRegionMaterialDetailsList(newDetails);
                                if(list.isEmpty()){
                                    CldProjectMaterial material = new CldProjectMaterial();
                                    material.setProjectId(obj.getProjectId());
                                    material.setCommodityId(updateObj.getCommodityId());
                                    cldProjectMaterialMapper.deleteCldProjectMaterialByProjectIdAndCommodityId(material);
                                }
                            }
                        }
                        //新增新项目物资阈值数据
                        CldRegionMaterialDetails oldDetails = new CldRegionMaterialDetails();
                        oldDetails.setRegionMaterialId(regionMaterialList.get(0).getRegionMaterialId());
                        List<CldRegionMaterialDetails> oldlist = cldRegionMaterialDetailsMapper.selectCldRegionMaterialDetailsList(oldDetails);
                        if(!oldlist.isEmpty()){
                            for (CldRegionMaterialDetails newMaterialDetails : oldlist) {
                                CldProjectMaterial projectMaterial = new CldProjectMaterial();
                                projectMaterial.setProjectId(obj.getProjectId());
                                projectMaterial.setCommodityId(newMaterialDetails.getCommodityId());
                                List<CldProjectMaterial> list = cldProjectMaterialMapper.selectCldProjectMaterialByProjectIdAndCommodityId(projectMaterial);
                                if(list.isEmpty()){
                                    CldProjectMaterial material = new CldProjectMaterial();
                                    material.setProjectId(obj.getProjectId());
                                    material.setCommodityId(newMaterialDetails.getCommodityId());
                                    material.setCommodityNum(500L);
                                    cldProjectMaterialMapper.insertCldProjectMaterial(material);
                                }
                            }
                        }
                    }
                }
            }
        }
        return cldRegionMaterialMapper.updateCldRegionMaterial(cldRegionMaterial);
    }

    /**
     * 批量删除业态物资清单
     * 
     * @param regionMaterialIds 需要删除的业态物资清单主键
     * @return 结果
     */
    @Override
    public int deleteCldRegionMaterialByRegionMaterialIds(Long[] regionMaterialIds)
    {
        return cldRegionMaterialMapper.deleteCldRegionMaterialByRegionMaterialIds(regionMaterialIds);
    }

    /**
     * 删除业态物资清单信息
     * 
     * @param regionMaterialId 业态物资清单主键
     * @return 结果
     */
    @Override
    public int deleteCldRegionMaterialByRegionMaterialId(Long regionMaterialId)
    {
        return cldRegionMaterialMapper.deleteCldRegionMaterialByRegionMaterialId(regionMaterialId);
    }
}
